{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function writelines:\n",
      "\n",
      "writelines(lines, /) method of _io.TextIOWrapper instance\n",
      "    Write a list of lines to stream.\n",
      "    \n",
      "    Line separators are not added, so it is usual for each of the\n",
      "    lines provided to have a line separator at the end.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "fp = open('D:\\\\1234.txt','w')\n",
    "fp = open('D:\\\\1234.txt','a')\n",
    "\n",
    "\n",
    "help(fp.writelines)\n",
    "fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "fp = open('D:\\\\1234.txt','w')\n",
    "text = ['1','2','3','4']\n",
    "text_2 = [i+'\\r\\n' for i in text[:-1]] + text[-1:]\n",
    "fp.writelines(text_2)\n",
    "fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "'copy' in os.__all__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on TextIOWrapper object:\n",
      "\n",
      "class TextIOWrapper(_TextIOBase)\n",
      " |  TextIOWrapper(buffer, encoding=None, errors=None, newline=None, line_buffering=False, write_through=False)\n",
      " |  \n",
      " |  Character and line based layer over a BufferedIOBase object, buffer.\n",
      " |  \n",
      " |  encoding gives the name of the encoding that the stream will be\n",
      " |  decoded or encoded with. It defaults to locale.getpreferredencoding(False).\n",
      " |  \n",
      " |  errors determines the strictness of encoding and decoding (see\n",
      " |  help(codecs.Codec) or the documentation for codecs.register) and\n",
      " |  defaults to \"strict\".\n",
      " |  \n",
      " |  newline controls how line endings are handled. It can be None, '',\n",
      " |  '\\n', '\\r', and '\\r\\n'.  It works as follows:\n",
      " |  \n",
      " |  * On input, if newline is None, universal newlines mode is\n",
      " |    enabled. Lines in the input can end in '\\n', '\\r', or '\\r\\n', and\n",
      " |    these are translated into '\\n' before being returned to the\n",
      " |    caller. If it is '', universal newline mode is enabled, but line\n",
      " |    endings are returned to the caller untranslated. If it has any of\n",
      " |    the other legal values, input lines are only terminated by the given\n",
      " |    string, and the line ending is returned to the caller untranslated.\n",
      " |  \n",
      " |  * On output, if newline is None, any '\\n' characters written are\n",
      " |    translated to the system default line separator, os.linesep. If\n",
      " |    newline is '' or '\\n', no translation takes place. If newline is any\n",
      " |    of the other legal values, any '\\n' characters written are translated\n",
      " |    to the given string.\n",
      " |  \n",
      " |  If line_buffering is True, a call to flush is implied when a call to\n",
      " |  write contains a newline character.\n",
      " |  \n",
      " |  Method resolution order:\n",
      " |      TextIOWrapper\n",
      " |      _TextIOBase\n",
      " |      _IOBase\n",
      " |      builtins.object\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __getstate__(...)\n",
      " |  \n",
      " |  __init__(self, /, *args, **kwargs)\n",
      " |      Initialize self.  See help(type(self)) for accurate signature.\n",
      " |  \n",
      " |  __next__(self, /)\n",
      " |      Implement next(self).\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  close(self, /)\n",
      " |      Flush and close the IO object.\n",
      " |      \n",
      " |      This method has no effect if the file is already closed.\n",
      " |  \n",
      " |  detach(self, /)\n",
      " |      Separate the underlying buffer from the TextIOBase and return it.\n",
      " |      \n",
      " |      After the underlying buffer has been detached, the TextIO is in an\n",
      " |      unusable state.\n",
      " |  \n",
      " |  fileno(self, /)\n",
      " |      Returns underlying file descriptor if one exists.\n",
      " |      \n",
      " |      OSError is raised if the IO object does not use a file descriptor.\n",
      " |  \n",
      " |  flush(self, /)\n",
      " |      Flush write buffers, if applicable.\n",
      " |      \n",
      " |      This is not implemented for read-only and non-blocking streams.\n",
      " |  \n",
      " |  isatty(self, /)\n",
      " |      Return whether this is an 'interactive' stream.\n",
      " |      \n",
      " |      Return False if it can't be determined.\n",
      " |  \n",
      " |  read(self, size=-1, /)\n",
      " |      Read at most n characters from stream.\n",
      " |      \n",
      " |      Read from underlying buffer until we have n characters or we hit EOF.\n",
      " |      If n is negative or omitted, read until EOF.\n",
      " |  \n",
      " |  readable(self, /)\n",
      " |      Return whether object was opened for reading.\n",
      " |      \n",
      " |      If False, read() will raise OSError.\n",
      " |  \n",
      " |  readline(self, size=-1, /)\n",
      " |      Read until newline or EOF.\n",
      " |      \n",
      " |      Returns an empty string if EOF is hit immediately.\n",
      " |  \n",
      " |  reconfigure(self, /, *, encoding=None, errors=None, newline=None, line_buffering=None, write_through=None)\n",
      " |      Reconfigure the text stream with new parameters.\n",
      " |      \n",
      " |      This also does an implicit stream flush.\n",
      " |  \n",
      " |  seek(self, cookie, whence=0, /)\n",
      " |      Change stream position.\n",
      " |      \n",
      " |      Change the stream position to the given byte offset. The offset is\n",
      " |      interpreted relative to the position indicated by whence.  Values\n",
      " |      for whence are:\n",
      " |      \n",
      " |      * 0 -- start of stream (the default); offset should be zero or positive\n",
      " |      * 1 -- current stream position; offset may be negative\n",
      " |      * 2 -- end of stream; offset is usually negative\n",
      " |      \n",
      " |      Return the new absolute position.\n",
      " |  \n",
      " |  seekable(self, /)\n",
      " |      Return whether object supports random access.\n",
      " |      \n",
      " |      If False, seek(), tell() and truncate() will raise OSError.\n",
      " |      This method may need to do a test seek().\n",
      " |  \n",
      " |  tell(self, /)\n",
      " |      Return current stream position.\n",
      " |  \n",
      " |  truncate(self, pos=None, /)\n",
      " |      Truncate file to size bytes.\n",
      " |      \n",
      " |      File pointer is left unchanged.  Size defaults to the current IO\n",
      " |      position as reported by tell().  Returns the new size.\n",
      " |  \n",
      " |  writable(self, /)\n",
      " |      Return whether object was opened for writing.\n",
      " |      \n",
      " |      If False, write() will raise OSError.\n",
      " |  \n",
      " |  write(self, text, /)\n",
      " |      Write string to stream.\n",
      " |      Returns the number of characters written (which is always equal to\n",
      " |      the length of the string).\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  buffer\n",
      " |  \n",
      " |  closed\n",
      " |  \n",
      " |  encoding\n",
      " |      Encoding of the text stream.\n",
      " |      \n",
      " |      Subclasses should override.\n",
      " |  \n",
      " |  errors\n",
      " |      The error setting of the decoder or encoder.\n",
      " |      \n",
      " |      Subclasses should override.\n",
      " |  \n",
      " |  line_buffering\n",
      " |  \n",
      " |  name\n",
      " |  \n",
      " |  newlines\n",
      " |      Line endings translated so far.\n",
      " |      \n",
      " |      Only line endings translated during reading are considered.\n",
      " |      \n",
      " |      Subclasses should override.\n",
      " |  \n",
      " |  write_through\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Methods inherited from _IOBase:\n",
      " |  \n",
      " |  __del__(...)\n",
      " |  \n",
      " |  __enter__(...)\n",
      " |  \n",
      " |  __exit__(...)\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  readlines(self, hint=-1, /)\n",
      " |      Return a list of lines from the stream.\n",
      " |      \n",
      " |      hint can be specified to control the number of lines read: no more\n",
      " |      lines will be read if the total size (in bytes/characters) of all\n",
      " |      lines so far exceeds hint.\n",
      " |  \n",
      " |  writelines(self, lines, /)\n",
      " |      Write a list of lines to stream.\n",
      " |      \n",
      " |      Line separators are not added, so it is usual for each of the\n",
      " |      lines provided to have a line separator at the end.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors inherited from _IOBase:\n",
      " |  \n",
      " |  __dict__\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open('123.txt','w') as fp:\n",
    "    help(fp)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "for i in range(1,1001):\n",
    "    open(f'j:\\\\old\\\\这是第{i}个mark文件.md','w')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "for md in os.listdir('j:\\\\old'):\n",
    "    new_md = md.replace('mark','markdown')\n",
    "    os.chdir('j:\\\\old')\n",
    "    os.rename(md,new_md)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function rename in module nt:\n",
      "\n",
      "rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n",
      "    Rename a file or directory.\n",
      "    \n",
      "    If either src_dir_fd or dst_dir_fd is not None, it should be a file\n",
      "      descriptor open to a directory, and the respective path string (src or dst)\n",
      "      should be relative; the path will then be relative to that directory.\n",
      "    src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n",
      "      If they are unavailable, using them will raise a NotImplementedError.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(os.rename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function nt.stat(path, *, dir_fd=None, follow_symlinks=True)>"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.stat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'j:\\\\old'"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.makedirs('\\\\new\\\\new')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function mkdir in module nt:\n",
      "\n",
      "mkdir(path, mode=511, *, dir_fd=None)\n",
      "    Create a directory.\n",
      "    \n",
      "    If dir_fd is not None, it should be a file descriptor open to a directory,\n",
      "      and path should be relative; path will then be relative to that directory.\n",
      "    dir_fd may not be implemented on your platform.\n",
      "      If it is unavailable, using it will raise a NotImplementedError.\n",
      "    \n",
      "    The mode argument is ignored on Windows.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(os.mkdir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['copyfileobj', 'copyfile', 'copymode', 'copystat', 'copy', 'copy2', 'copytree', 'move', 'rmtree', 'Error', 'SpecialFileError', 'ExecError', 'make_archive', 'get_archive_formats', 'register_archive_format', 'unregister_archive_format', 'get_unpack_formats', 'register_unpack_format', 'unregister_unpack_format', 'unpack_archive', 'ignore_patterns', 'chown', 'which', 'get_terminal_size', 'SameFileError', 'disk_usage']\n"
     ]
    }
   ],
   "source": [
    "import shutil\n",
    "print(shutil.__all__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function copystat in module shutil:\n",
      "\n",
      "copystat(src, dst, *, follow_symlinks=True)\n",
      "    Copy file metadata\n",
      "    \n",
      "    Copy the permission bits, last access time, last modification time, and\n",
      "    flags from `src` to `dst`. On Linux, copystat() also copies the \"extended\n",
      "    attributes\" where possible. The file contents, owner, and group are\n",
      "    unaffected. `src` and `dst` are path names given as strings.\n",
      "    \n",
      "    If the optional flag `follow_symlinks` is not set, symlinks aren't\n",
      "    followed if and only if both `src` and `dst` are symlinks.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(shutil.copystat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "ename": "PermissionError",
     "evalue": "[Errno 13] Permission denied: 'j:\\\\new\\\\这是第1个markdown文件.md'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mPermissionError\u001b[0m                           Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-109-b04f1939f165>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mshutil\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcopy2\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'j:\\\\old\\\\这是第1个markdown文件.md'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'j:\\\\new'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mc:\\users\\feng_sun\\appdata\\local\\programs\\python\\python37-32\\lib\\shutil.py\u001b[0m in \u001b[0;36mcopy2\u001b[1;34m(src, dst, follow_symlinks)\u001b[0m\n\u001b[0;32m    264\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0misdir\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdst\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    265\u001b[0m         \u001b[0mdst\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdst\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbasename\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msrc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 266\u001b[1;33m     \u001b[0mcopyfile\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msrc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdst\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfollow_symlinks\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfollow_symlinks\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    267\u001b[0m     \u001b[0mcopystat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msrc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdst\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfollow_symlinks\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfollow_symlinks\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    268\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mdst\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\feng_sun\\appdata\\local\\programs\\python\\python37-32\\lib\\shutil.py\u001b[0m in \u001b[0;36mcopyfile\u001b[1;34m(src, dst, follow_symlinks)\u001b[0m\n\u001b[0;32m    119\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    120\u001b[0m         \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msrc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'rb'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mfsrc\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 121\u001b[1;33m             \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdst\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'wb'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mfdst\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    122\u001b[0m                 \u001b[0mcopyfileobj\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfsrc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfdst\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    123\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mdst\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mPermissionError\u001b[0m: [Errno 13] Permission denied: 'j:\\\\new\\\\这是第1个markdown文件.md'"
     ]
    }
   ],
   "source": [
    "shutil.copy2('j:\\\\old\\\\这是第1个markdown文件.md','j:\\\\new')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'j:\\\\old'"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<module 'ntpath' from 'c:\\\\users\\\\feng_sun\\\\appdata\\\\local\\\\programs\\\\python\\\\python37-32\\\\lib\\\\ntpath.py'>"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
